app.controller("z-holiday-c", [ '$scope','$rootScope','$z-http','$sce','z-holiday-s','z-edit-menu-s','$filter','$location','downloadUrl',
		function($scope,$rootScope,$zhttp,$sce,Service,MenuService,$filter,$location,$downloadUrl) {
		$scope.baseUrl = $location.protocol()+"://"+$location.host()+":"+$location.port()+$downloadUrl;
		$scope.viewModel = {};
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";	
		$scope.viewModel.yearData = [];
		var newYear = new Date().getFullYear();
		for(var year = newYear-10;year<=newYear+10;year++ ){
			$scope.viewModel.yearData.push(year);
		}
		$scope.viewModel.holidayYear = newYear+"";
	
		$scope.editModel = {};
		$scope.editModel.title="";
		
		//查询页面名称
		MenuService.getMenuByUrl({url:'holiday'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getHolidayListByPageCount({
				pageNo:$scope.viewModel.rowCount,
				pageCount:$scope.viewModel.perPage,
				holidayYear:$scope.viewModel.holidayYear,
			},
			function(data){
				$scope.viewModel.dataList = data.holidayBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		
		$scope.viewModel.bindData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
		};
		
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
		};

		
        $scope.viewModel.exportData= function() {
        	
        };
		
        //模板下载
        $scope.viewModel.getHolidayURL = function () {
            $rootScope.isLoading = true;
            Service.getHolidayURL(function (data) {
                $rootScope.isLoading = false;
                window.location.href       = data;
            }, function () {
                $rootScope.isLoading = false;
            });
        };

		$scope.importModel = {};
		
        $scope.importModel.attachmentId = null;
        $scope.importModel.uploadStream = {};
        $scope.importModel.messageHideType=false;
        $scope.importModel.successHideType=false;
        $scope.importModel.message=null;
        $scope.importModel.isArray=false;
        $scope.importModel.radioValue="2";

		
        $scope.importModel.getTemplate = function () {
            $rootScope.isLoading = true;
            Service.getTemplateUrl(function (data) {
                $rootScope.isLoading = false;
                window.location.href       = data;
            }, function () {
                $rootScope.isLoading = false;
                //$rootScope.alertError("系统错误!");
                $scope.importModel.messageHideType=true;
                $scope.importModel.message="系统错误";
                $scope.importModel.isArray=false;
            });
        };

        $scope.importModel.unload = function () {
            $scope.importModel.attachmentId = null;
            $scope.importModel.uploadStream.cancel();
        };

        $scope.importModel.fileUploadComplete    = function (data) {
            $scope.importModel.messageHideType=false;
            $scope.importModel.successHideType=false;
            var dataList=data;
            if (dataList[0]['attachment'] != null) {
                $scope.$apply(function () {
                    $scope.importModel.attachmentId = dataList[0]['attachment']['attachmentId'];
                    $scope.importModel.save();
                });
            }
        };

        $scope.importModel.save = function () {
            var str="确定是否增量导入！";
            if($scope.importModel.radioValue=="1"){
                str="覆盖导入会根据编码替换原有信息，确认是否导入！";
            }
            $rootScope.alertConfirm(str,
                function() {
                    if ($scope.importModel.attachmentId != null) {
                        $rootScope.isLoading = true;
                        Service.importHoliday({
                            attachmentId: $scope.importModel.attachmentId,
                            type:$scope.importModel.radioValue
                        }, function (data) {
                            $rootScope.isLoading = false;
                            if (data['result'] == 1) {
                                $scope.importModel.successHideType=true;
                                if(data['success'].length<=0){
                                    $scope.importModel.isArray = false;
                                    $scope.importModel.message = "员工信息导入完成。";
                                }else{
                                    $scope.importModel.isArray = true;
                                    $rootScope.alertInfo(data.success[0]);
                                }
                                $scope.importModel.unload();
                            } else {
                                $scope.importModel.messageHideType=true;
                                $scope.importModel.message="系统错误";
                                $scope.importModel.isArray=false;
                            }
                        }, function (data) {
                            $rootScope.isLoading      = false;
                            $scope.importModel.attachmentId = null;
                            $scope.importModel.uploadStream.cancel();
                            $scope.importModel.messageHideType=true;
                            if (angular.isArray(data['fault'])) {
                                var faultMessage = [data['fault'][0]['message']].concat(data['fault'][0]['errorData']);
                                $rootScope.alertError(faultMessage);
                                $scope.importModel.isArray=true;
                                //$rootScope.alertError(faultMessage);
                            } else {
                            	$rootScope.alertError(data['fault']['message']);
                                $scope.importModel.isArray=false;
                                //$rootScope.alertError(data['fault']['message']);
                            }
                        });
                    } else {
                        $scope.importModel.message="请先上传文件!";
                        $scope.importModel.isArray=false;
                    }
                });
        };
		$scope.importModel.del=function(holiday){
			$rootScope.alertConfirm("确定删除吗？",function() {
				$scope.viewModel.isLoading=true;
				Service.deleteHoliday({
					holidayId:holiday.holidayId
				},function(){
					$scope.viewModel.bindData();
					$scope.viewModel.isLoading=false;
					$rootScope.alertInfo("删除成功。");
				},function(error){
					$scope.viewModel.isLoading=false;
					$rootScope.alertError(error.fault.message);
				});
			});
		};
        
        
        
} ]);